import { RoleService } from '@/services/role';

export default {
  state: {
    list: [],
  },
  effects: {
    *init({}, { call, put, select }) {
      //先用select函数拿到仓库中当前的list
      let list = yield select(state => state.role.list);
      //判断是不是初始化过，是就返回，不往后面走了
      if (list.length > 0) {
        return;
      }
      //如果没有初始化过，把发ajax请求的函数放到call中，call会自动执行这个函数，并把结果返回给list
      list = yield call(RoleService.queryRoleList);
      //自己在调用put触发
      yield put({ type: '_init', payload: list });
    },
  },
  reducers: {
    _init(state, { payload: list }) {
      return { list };
    },
  },
};
